<template>
    <div>
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 主题内容 -->
        <el-card>
            <el-row>
                <el-col>
                    <el-input
                    placeholder="请输入内容" 
                    v-model="queryInfo.query" 
                    class="input-with-select"
                    clearable
                    @clear="getOrderLists">
                        <el-button slot="append" icon="el-icon-search" @click="getOrderLists"></el-button>
                    </el-input>
                </el-col>
            </el-row>
                <el-table
                    :data="orderTableData"
                    border
                    style="width: 100%">
                    <!-- 索引列 -->
                    <el-table-column
                    type="index"
                    label="#"
                    align="center">
                    </el-table-column>
                    <el-table-column
                    prop="order_number"
                    label="订单编号"
                    align="left">
                    </el-table-column>
                    <el-table-column
                    prop="order_price"
                    label="订单价格"
                    width="100"
                    align="center">
                    </el-table-column>
                    <el-table-column
                    prop="order_pay"
                    label="是否付款"
                    width="100"
                    align="center">
                        <template slot-scope="scope">
                            <el-tag type="danger" v-if="scope.row.order_pay === '0'" size="mini">未付款</el-tag>
                            <el-tag type="success" v-else size="mini">已付款</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column
                    prop="is_send"
                    label="是否发货"
                    width="100"
                    align="center">
                    </el-table-column>
                    <el-table-column
                    prop="create_time"
                    label="下单时间"
                    width="200"
                    align="center">
                    <!-- 自定义时间，用全局过滤器 -->
                    <template slot-scope="scope">
                        {{scope.row.create_time | dateFormat}}
                    </template>
                    </el-table-column>
                    <el-table-column
                    label="操作"
                    width="200"
                    align="center">
                        <template>
                            <!-- 修改地址的按钮 -->
                            <el-tooltip class="item" effect="dark" content="修改用户收货地址" placement="top">
                                <el-button type="primary" icon="el-icon-edit" size="mini" @click="editAddr"></el-button>
                            </el-tooltip>
                            <!-- 物流进度 -->
                            <el-tooltip class="item" effect="dark" content="物流进度" placement="top">
                                <el-button type="success" icon="el-icon-location-outline" size="mini" @click="showProgressBox"></el-button>
                            </el-tooltip>
                            
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页功能 -->
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
                
                <!-- 点击修改地址弹出的对话框 -->
                <el-dialog
                    title="修改地址"
                    :visible.sync="addressDialogVisible"
                    width="50%"
                    @close="addressDialogClosed">
                    <el-form :model="addressForm" :rules="addressFormRules" ref="ruleAddressFormRef" label-width="100px" class="demo-ruleForm">
                        <el-form-item label="省市区/县" prop="address1">
                            <el-cascader
                                v-model="addressForm.address1"
                                :options="cityData"
                                :props="{ expandTrigger: 'hover' }"
                                @change="addressHandleChange"
                                clearable ></el-cascader>
                        </el-form-item>
                        <el-form-item label="详细地址" prop="address2">
                            <el-input v-model="addressForm.address2"></el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="addressDialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
                    </span>
                </el-dialog>

                <!-- 显示物流进度的对话框 -->
                <el-dialog
                    title="物流进度"
                    :visible.sync="progressDialogVisible"
                    width="50%"
                    @close="progressDialogClosed">
                    <!-- 时间线 -->
                    <el-timeline>
                        <el-timeline-item
                        v-for="(activity, index) in progressInfo"
                        :key="index"
                        :timestamp="activity.time">
                        {{activity.context}}
                        </el-timeline-item>
                    </el-timeline>
                   
                </el-dialog>
        </el-card>
    </div>
</template>

<script>
    // 获取省市区的地址
    import cityData from './citydata'
    // 引入物流信息
    import wuliu from './wuliu'
    export default {
        name: 'Orders',
        data() {
            return {
                queryInfo:{ // 获取订单列表的参数对象
                    query:'',
                    pagenum : 1, // 当前的页码  不能是字符串
                    pagesize : 10 // 每页显示条数
                },
                orderTableData: [], //表格的数据
                total:0, //一共有多少条数据
                addressDialogVisible: false, // 显示或隐藏修改地址的对话框
                addressForm:{ // 获取用户地址
                    address1: [],
                    address2: ''
                },
                addressFormRules:{ // 修改地址的表单验证规则
                    address1:[
                        {
                            required: true,
                            message: '请选择省市区/县',
                            trigger: 'blur'
                        }
                    ],
                    address2:[
                        {
                            required: true,
                            message: '请输入详细地址',
                            trigger: 'blur'
                        }
                    ]
                },
                cityData: cityData, // 省市区的地址
                progressDialogVisible: false, // 显示或隐藏物流进度的对话框
                wuliu:wuliu, //引入物流信息数据，后面要重新赋值
                progressInfo:'', //物流数据，由wuliu传过来的
            }
        },
        created(){
            this.getOrderLists()    // 获取订单列表的数据
        },
        methods:{
            async getOrderLists(){ // 获取订单列表的数据
                const {data:res} = await this.$http.get('orders',{params:this.queryInfo})
                console.log(res);
                if(res.meta.status != 200) return this.$message.error(res.meta.msg)
                this.orderTableData = res.data.goods
                this.total = res.data.total
            },
            // 分页功能中选择一页显示多少条
            handleSizeChange(newSize){
                // console.log(newSize);
                this.queryInfo.pagesize = newSize
                this.getOrderLists()
            },
            // 分页功能中跳到哪一页
            handleCurrentChange(newPage){
                // console.log(newPage);
                this.queryInfo.pagenum = newPage
                this.getOrderLists()
            },
            // 修改地址对话框的关闭事件
            addressDialogClosed(){
                this.$refs.ruleAddressFormRef.resetFields()
            },
            // 修改用户收货地址的函数
            editAddr(){
                this.addressDialogVisible = true
            },
            // 修改地址对话框中级联选择器发生变化触发的函数
            addressHandleChange(){

            },
            // 查询物流进度的确定事件
            showProgressBox(){
                // 对物流信息重新赋值
                // console.log(this.wuliu);
                this.progressInfo = this.wuliu[0].data
                this.progressDialogVisible = true
                // console.log(this.progressInfo);
            },
            // 物流进度对话框的关闭事件
            progressDialogClosed(){
                
            }
        }
    }
</script>

<style scoped>
.input-with-select{
    width: 350px;
}
.el-cascader{
    width: 100%;
}
</style>